package edu.depaul.se.courseregistration.jpa.courses;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;

@NamedQueries({
    @NamedQuery(name=  Subject.SUBJECT_QUERY_ALL,query="select s from Subject s"),
    @NamedQuery(name = Subject.SUBJECT_QUERY_BY_ID, query = "SELECT s FROM Subject s WHERE s.subjectID = :" + Subject.SUBJECT_ID_PARAMETER_NAME), //Find subject By ID.
    @NamedQuery(name = Subject.SUBJECT_QUERY_BY_COLLEGE_ID, query = "SELECT s FROM Subject s WHERE s.collegeId = :" + Subject.COLLEGE_ID_PARAMETER_NAME), //Find College By ID.
    @NamedQuery(name = Subject.SUBJECT_QUERY_BY_CODE, query = "SELECT s FROM Subject s WHERE s.subjectCode = :" + Subject.SUBJECT_CODE_PARAMETER_NAME) //Find subject By Code.


})
@Entity
@Table(name = "SUBJECT")
/**
 *
 * @author lmarco
 */
public class Subject implements Serializable { 
    private static final long serialVersionUID = 1L;

    public static final String SUBJECT_QUERY_ALL = "Subject.findAll";
    public static final String SUBJECT_QUERY_BY_COLLEGE_ID = "Subject.findByCollege";
    public static final String SUBJECT_QUERY_BY_ID = "Subject.findById";
    public static final String SUBJECT_ID_PARAMETER_NAME = "subjectID";
    public static final String SUBJECT_QUERY_BY_CODE = "Subject.findByCode";
    public static final String SUBJECT_CODE_PARAMETER_NAME = "subjectCode";
    public static final String COLLEGE_ID_PARAMETER_NAME = "collegeId";

    
    
    @Id
    @Column(name = "SUBJECT_ID")
    private Integer subjectID;
    @Column(name = "SUBJECT_CODE")
    private String subjectCode;
    @Column(name = "SUBJECT_DESCRIPTION")
    private String subjectDescription;
    
   @OneToMany(cascade = CascadeType.ALL, 
                mappedBy = "subjectID", 
                fetch= FetchType.EAGER)
    //@OrderBy("subjectCode DSC")
    private List<Course> courseList;
    @JoinColumn(name = "COLLEGE_ID", referencedColumnName = "COLLEGE_ID")
    @ManyToOne(optional = false)  
    private College collegeId; //update back  to  College as the relationship is with entity compile fail

    @XmlTransient
    public College getCollegeId() { 
        return collegeId;
    }

    public void setCollegeId(College collegeId) {
        this.collegeId = collegeId;
    }

    @XmlElement(name="courseList")
    public List<Course> getCourseList() {
        return courseList;
    }

    public void setCourseList(List<Course> courseList) {
        this.courseList = courseList;
    }

    public Integer getSubjectID() {
        return subjectID;
    }

    public void setSubjectID(Integer subjectID) {
        this.subjectID = subjectID;
    }

    public String getSubjectCode() {
        return subjectCode;
    }

    public void setSubjectCode(String subjectCode) {//update method name Feb 11 -Xiao
        this.subjectCode = subjectCode;
    }

     public String getSubjectDescription() {
        return subjectDescription;
    }

    public void setSubjectDescription(String subjectDescription) {
        this.subjectDescription = subjectDescription;
    }
   
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (subjectID != null ? subjectID.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Course)) {
            return false;
        }
        Subject other = (Subject) object;
        if ((this.subjectID == null && other.subjectID != null) || (this.subjectID != null && !this.subjectID.equals(other.subjectID))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Subject[ subjectCode=");
        sb.append(subjectCode);
        return sb.toString();
    }
}
